﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Taurus.Parking.DTO;
using Taurus.Platform.XML;
using System.Xml.Linq;

namespace Taurus.Parking.XML
{
    class ConfigPersistence : BasePersistence<Config>
    {
        public override string FileName
        {
            get { return "ParkingConfig.xml"; }
        }

        public override XElement GetEl(Config item)
        {
            var t = (from el in Root.Elements("Config") where el.Attribute("UID").Value == item.UID select el).FirstOrDefault();
            return t;
        }

        public override Config GetObject(string id)
        {
            Config config = new Config(id);
            return XChange(GetEl(config));
        }

        protected override XElement XChange(Config item)
        {
            XElement el = new XElement("Config");
            el.SetAttributeValue("UID", item.UID);
            el.SetAttributeValue("IsAutoParking", item.IsAutoParking);
            el.SetAttributeValue("AutoUpdateInterval", item.AutoUpdateInterval);
            return el;
        }

        protected override Config XChange(XElement item)
        {
            if (item!=null)
            {
                Config cf = new Config(item.Attribute("UID").Value)
                {
                    IsAutoParking = bool.Parse(item.Attribute("IsAutoParking").Value),
                    AutoUpdateInterval = int.Parse(item.Attribute("AutoUpdateInterval").Value)
                };
                return cf;
            }
            return null;
        }
    }
}
